package com.ciban.smarthome.module.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.ciban.smarthome.R;
import com.ciban.smarthome.common.utils.DensityUtils;
import com.ciban.smarthome.common.utils.ImgUtils;
import com.ciban.smarthome.common.utils.StringUtils;
import com.ciban.smarthome.module.ui.base.BaseActivity;
import com.ciban.smarthome.module.db.dao.SceneDao;
import com.ciban.smarthome.module.entity.Scene;

public class SceneModifyActivity extends BaseActivity implements
		OnClickListener {
	private static final int REQUEST_CONDE_CHOOSEICON = 1;

	private EditText et_no, et_name, et_host;
	private ImageView iv_icon;
	private Button bt_cancle, bt_save;

	private String icon_path = "imgs/light_icon_bd.png";



	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_scene_modify);

		int width = DensityUtils.getWindowWidth(this);
		LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数值
		p.width = (int) (width * 0.8); // 宽度设置为屏幕的0.8
		getWindow().setAttributes((android.view.WindowManager.LayoutParams) p);

		initView();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case REQUEST_CONDE_CHOOSEICON:
			icon_path = data.getStringExtra("path");
			iv_icon.setImageBitmap(ImgUtils.getImageFromAssetsFile(this,
					icon_path));
			break;
		}
	}


	private void initView() {
		et_no = (EditText) findViewById(R.id.scene_add_no);
		et_name = (EditText) findViewById(R.id.scene_add_name);
		iv_icon = (ImageView) findViewById(R.id.scene_add_icon);
		bt_cancle = (Button) findViewById(R.id.scene_add_cancle);
		bt_save = (Button) findViewById(R.id.scene_add_save);
		et_host = (EditText) findViewById(R.id.scene_add_host);

		setListeners();
	}

	private void setListeners() {
		iv_icon.setOnClickListener(this);
		bt_cancle.setOnClickListener(this);
		bt_save.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.scene_add_icon:
			Intent intent = new Intent(this, IconChooseActivity.class);
			intent.putExtra("ids",
					getResources().getStringArray(R.array.scene_icons));
			startActivityForResult(intent, REQUEST_CONDE_CHOOSEICON);
			break;
		case R.id.scene_add_cancle:
			finish();
			break;
		case R.id.scene_add_save:
			addNewScene();
			break;
		}
	}

	private void addNewScene() {
		String no = et_no.getText().toString().trim();
		String name = et_name.getText().toString().trim();
		String host_serno=et_host.getText().toString().trim();
		if (StringUtils.isEmpty(no)) {
			displayShort("编号输入错误");
			return;
		}

		if (StringUtils.isEmpty(name)) {
			displayShort("名称不能为空");

			return;
		}

		if (StringUtils.isEmpty(host_serno)){
			displayShort("主机编号不能为空");
			return;
		}


		Scene info = new Scene();
		info.setSerial_no(Integer.parseInt(no));
		info.setName(name);
		info.setIcon_path(icon_path + "");
		info.setHost_serno(host_serno);

		SceneDao dao = new SceneDao(this);
		long id = dao.addNewScene(info);
		if (id >= 0) {
			setResult(RESULT_OK);
			finish();
		} else {
			displayShort("添加失败");
		}
		dao = null;
	}
}
